/**
 * 用于缓存用户信息，避免频繁请求
 */
import { createContext, useContext, useState } from 'react'

const userInfoContext = createContext()

export const UserInfoProvider = ({ children }) => {
  const [userInfo, setUserInfo] = useState({
    userId: 0,
    username: '',
    phone: '',
    email: '',
    avatar: ''
  })

  return (
    <userInfoContext.Provider value={{ userInfo, setUserInfo }}>
      {children}
    </userInfoContext.Provider>
  )
}

// 自定义hook
export const useUserInfo = () => {
  const context = useContext(userInfoContext)
  if (!context) {
    throw new Error('useUserInfo must be used within a UserInfoProvider')
  }
  return context
}